package com.fhc.view

import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.layout.AlignmentLine
import androidx.compose.ui.layout.FirstBaseline
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.layout
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.LayoutDirection


/**
 * 实现了一个自定义垂直排列的 Compose 布局组件，功能如下：
 * 使用 Layout 自定义布局逻辑；
 * 依次测量子组件高度并累加，宽度取最大值；
 * 根据 layoutDirection 决定子组件从上往下（Ltr）或从下往上（Rtl）排列。
 * 示例场景： 假设你有三个子组件：A (高 50px)、B (高 100px)、C (高 75px)，并且 layoutDirection 为 Ltr：
 * 整体高度为 50 + 100 + 75 = 225px，宽度取最大值（例如 C 的宽度为 150px）。
 * A 放置在 y=0，B 放置在 y=50，C 放置在 y=150。
 * @param modifier
 * @param layoutDirection
 * @param content
 * @receiver
 */
@Composable
fun CustomColumnView(
    modifier: Modifier = Modifier,
    layoutDirection: LayoutDirection,
    content: @Composable() () -> Unit
) {

    Layout(
        modifier = modifier,
        content = content
    ) { measurable, constraints ->
        var totalHeight = 0
        var maxWidth = 0
        val placeable = measurable.map {
            val placeable = it.measure(constraints)
            totalHeight += placeable.height
            if (placeable.width > maxWidth) {
                maxWidth = placeable.width
            }
            placeable
        }

        layout(maxWidth, totalHeight) {
            if (layoutDirection == LayoutDirection.Ltr) {
                var y = 0
                placeable.forEach {
                    it.place(0, y)
                    y += it.height
                }

            } else {
                var y = totalHeight
                placeable.forEach {
                    y -= it.height
                    it.place(0, y)
                }
            }
        }
    }
}

/**
 * First baseline to top
 *
 * @param firstBaselineToTop
 *///https://developer.android.google.cn/develop/ui/compose/layouts/custom?hl=zh-cn
fun Modifier.firstBaselineToTop(
    firstBaselineToTop: Dp
) = layout { measurable, constraints ->
    // Measure the composable
    val placeable = measurable.measure(constraints)

    // Check the composable has a first baseline
    check(placeable[FirstBaseline] != AlignmentLine.Unspecified)
    val firstBaseline = placeable[FirstBaseline]

    // Height of the composable with padding - first baseline
    val placeableY = firstBaselineToTop.roundToPx() - firstBaseline
    val height = placeable.height + placeableY
    layout(placeable.width, height) {
        // Where the composable gets placed
        placeable.placeRelative(0, placeableY)
    }
}

/**
 * Alpha anim
 *
 * @param durationMillis
 * @return
 */
@Composable
fun Modifier.alphaAnim(
    durationMillis: Int
): Modifier = composed {
    val infiniteTransition = rememberInfiniteTransition(label = "ProgressTransition")
    val alphaAnim by infiniteTransition.animateFloat(
        initialValue = 0.0f,
        targetValue = 1.0f,
        animationSpec = infiniteRepeatable(
            animation = tween(durationMillis, easing = LinearEasing),
            repeatMode = RepeatMode.Reverse
        ),
        label = "Progress"
    )
    this.alpha(alphaAnim)
}

